﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Data;
using MockPrjTeam2.Common;
using MockPrjTeam2.Entity;

namespace MockPrjTeam2.DAO
{
    public class ProgrammesDAO:Ketnoi
    {
        #region[Getall]
        public List<ProgrammesInfor> GetAll()
        {
            List<ProgrammesInfor> lst = new List<ProgrammesInfor>();
            using (SqlCommand cmd = new SqlCommand("sp_Programme_Getall",connect()))
            {
                ProgrammesInfor obj = new ProgrammesInfor();
                cmd.CommandType = CommandType.StoredProcedure;
                SqlDataReader dr = cmd.ExecuteReader();
                if (dr.HasRows)
                {
                    while (dr.Read())
                    {
                        lst.Add(obj.MappingEntity(dr));
                    }
                }
                dr.Close();
                obj = null;
            }
            return lst;
        }
        #endregion
        #region[GetByNameStartWith]
        public List<ProgrammesInfor> GetByNameStartWith(string startWith)
        {
            List<ProgrammesInfor> list = new List<ProgrammesInfor>();
            using (SqlCommand dbCmd = new SqlCommand("sp_GetByProgrammesName", connect()))
            {
                ProgrammesInfor obj = new ProgrammesInfor();
                dbCmd.CommandType = CommandType.StoredProcedure;
                dbCmd.Parameters.Add(new SqlParameter("@FirtName", startWith));
                SqlDataReader dr = dbCmd.ExecuteReader();
                if (dr.HasRows)
                {
                    while (dr.Read())
                    {
                        list.Add(obj.MappingEntity(dr));
                    }
                }
                dr.Close();
                obj = null;
            }
            return list;
        }
        #endregion
        #region[Programme_delete]
        public bool Delete(string id)
        {
            using (SqlCommand cmd = new SqlCommand("sp_programme_Delete", connect()))
            {
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@ProgrammeID", id));
                cmd.ExecuteNonQuery();
            }
            return true;
        }
        #endregion
        #region[update]
        public bool Update(ProgrammesInfor data)
        {
            using (SqlCommand cmd = new SqlCommand("sp_Programme_Update", connect()))
            {
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@ProgrammeName", data.ProgrammeName));
                cmd.Parameters.Add(new SqlParameter("@Des", data.Description));
                cmd.Parameters.Add(new SqlParameter("@Contact", data.ContactID));
                cmd.ExecuteNonQuery();
            }
            return true;
        }
        #endregion
        #region[insert]
        public bool Insert(ProgrammesInfor data)
        {
            using (SqlCommand cmd = new SqlCommand("sp_programme_insert", connect()))
            {
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@ProgrammeName", data.ProgrammeName));
                cmd.Parameters.Add(new SqlParameter("@Des", data.Description));
                cmd.Parameters.Add(new SqlParameter("@ContactID", data.ContactID));
                cmd.ExecuteNonQuery();
            }
            return true;
        }
        #endregion
        #region[ByID]
        public List<Entity.ProgrammesInfor> GetById(string id)
        {
            List<ProgrammesInfor> lst = new List<ProgrammesInfor>();
            using (SqlCommand cmd = new SqlCommand("Sp_Programme_GetByID", connect()))
            {
                ProgrammesInfor obj = new ProgrammesInfor();
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@PrID", id));
                SqlDataReader dr = cmd.ExecuteReader();
                if (dr.HasRows)
                {
                    while (dr.Read())
                    {
                        lst.Add(obj.MappingEntity(dr));
                    }
                }
                dr.Close();
                obj = null;
            }
            return lst;
        }
        #endregion
        #region[ByName]
        public List<Entity.ProgrammesInfor> GetByName(string name)
        {
            List<ProgrammesInfor> lst = new List<ProgrammesInfor>();
            using (SqlCommand cmd = new SqlCommand("Sp_Programme_GetName", connect()))
            {
                ProgrammesInfor obj = new ProgrammesInfor();
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@ProgrammeName", name));
                SqlDataReader dr = cmd.ExecuteReader();
                if (dr.HasRows)
                {
                    while (dr.Read())
                    {
                        lst.Add(obj.MappingEntity(dr));
                    }
                }
                dr.Close();
                obj = null;
            }
            return lst;
        }
        #endregion
    }
}
